package com.info.zhiduoduo.repository.mapper.system;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.info.zhiduoduo.repository.dto.system.QueryManagerDto;
import com.info.zhiduoduo.repository.entity.system.SystemManager;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface ManagerMapper extends BaseMapper<SystemManager> {

    @Select(" select * from zdd_system_manager where loginname = #{loginname}")
    SystemManager getManager(String loginname);

    /**
     * 查看角色被账户绑定的数量
     * @param roleid
     * @return
     */
    @Select("select COUNT(*) from zdd_system_manager where roleid = #{roleid}")
    int queryManagerCount(@Param("roleid") int roleid);

    /**
     * 查看账户列表
     * @return
     */
    @Select("select zsm.*,zsr.rolename from zdd_system_manager zsm LEFT JOIN zdd_system_role zsr ON zsm.roleid = zsr.roleid")
    List<QueryManagerDto> queryManagerPage();

    /**
     * 查看账户详情
     * @param managerid
     * @return
     */
    @Select("select zsm.*,zsr.rolename from zdd_system_manager zsm " +
            "LEFT JOIN zdd_system_role zsr ON zsm.roleid = zsr.roleid where zsm.managerid = #{managerid}")
    QueryManagerDto getManagerById(@Param("managerid") int managerid);

    /**
     * 校验登录名
     * @param loginname
     * @return
     */
    @Select("select * from zdd_system_manager where loginname = #{loginname}")
    QueryManagerDto checkLoginName(@Param("loginname") String loginname);


    /**
     * 登录
     * @param loginname
     * @return
     */
    @Select(" select m.*,r.rolename,group_concat(rm.menuid) menuids " +
            " from zdd_system_manager m " +
            " left join zdd_system_role r on r.roleid = m.roleid " +
            " left join zdd_system_rolemenu rm on rm.roleid = m.roleid " +
            " where loginname = #{loginname} " +
            " and password = #{password}")
    QueryManagerDto loginCheckPassword(@Param("loginname") String loginname, @Param("password") String password);

    /**
     * 登录
     * @param loginname
     * @return
     */
    @Select(" select m.*,r.rolename,group_concat(rm.menuid) menuids " +
            " from zdd_system_manager m " +
            " left join zdd_system_role r on r.roleid = m.roleid " +
            " left join zdd_system_rolemenu rm on rm.roleid = m.roleid " +
            " where loginname = #{loginname}")
    QueryManagerDto login(@Param("loginname") String loginname);

    /**
     * 冻结账户 解冻
     * @param managerid
     * @param state
     * @return
     */
    @Update("update zdd_system_manager set state = #{state} where managerid = #{managerid}")
    int updateManagerState(@Param("managerid") int managerid, @Param("state") int state);

    /**
     * 查询所有的管理员
     * @return
     */
    @Select("select * from zdd_system_manager where state = 1")
    List<SystemManager> selectAllSystemManager();
}
